package com.szqhyxj.youxiangju.biz.partnerHome.shop.purchase;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.CommoditeDetails;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.pay.AliPayActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.pay.PaySuccessfulActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.site.MySiteActivity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PartnerPreparePurchaseActivity extends BaseActivity implements NavigationBar.NavigationBarListener, PartnerPreparePurchaseContract.View{

    private static final String COMMODITEDETAILS = "CommoditeDetails";
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;

    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.commodite_title)
    TextView commoditeTitle;
    @BindView(R.id.price)
    TextView price;


    @BindView(R.id.choose)
    CheckBox choose;
    @BindView(R.id.partnership_agreement_symbol)
    TextView partnershipAgreementSymbol;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.quantity)
    TextView quantity;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.user_site)
    TextView userSite;
    @BindView(R.id.site)
    RelativeLayout site;
    @BindView(R.id.add_site)
    LinearLayout addSite;
    @BindView(R.id.pay)
    Button pay;
    @BindView(R.id.combined_price)
    TextView combinedPrice;

    private PartnerPreparePurchasePresenter presenter;

    public static void start(Activity activity, CommoditeDetails details){
        Intent intent = new Intent();
        intent.setClass(activity, PartnerPreparePurchaseActivity.class);
        intent.putExtra(COMMODITEDETAILS, details);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prepare_purchase);
        ButterKnife.bind(this);
        CommoditeDetails details =  getIntent().getParcelableExtra(COMMODITEDETAILS);
        navigationBar.setNavigationBarListener(this);
        presenter = new PartnerPreparePurchasePresenter(this);
        presenter.init(details);
        initEvent();
        presenter.initSite();
    }

    @Override
    public void onActivityReenter(int resultCode, Intent data){
        super.onActivityReenter(resultCode, data);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode){
            case MySiteActivity.START_100:
                if(resultCode == MySiteActivity.START_100){
                    presenter.initSite(data.getStringExtra("addrId"));
                }else{
                    presenter.initSite();
                }
                break;
            case AliPayActivity.CODE_2001:
                presenter.getUserInfo();
                PaySuccessfulActivity.start(this, "",0);
                break;
            case AliPayActivity.CODE_2002:
                break;
        }
    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    public void initEvent(){
        choose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b){
                presenter.onChecked(b);
                if(b){
                    pay.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                }else{
                    pay.setBackgroundColor(getResources().getColor(R.color.colorGray));
                }
            }
        });

    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }

    @Override
    public void showCommodite(CommoditeDetails data){
        LoadImageUtils.loadImage(icon, data.getSmallPic());
        commoditeTitle.setText(data.getPackageName());
        type.setText(data.getBrandName());
        quantity.setText("/" + data.getDeviceNum() + "台");
        price.setText(StringUtils.initPrice(data.getPackagePrice()));
        combinedPrice.setText(StringUtils.initPrice(data.getPackagePrice()));
    }

    @Override
    public void showSite(DefaultSite defaultSite){
        addSite.setVisibility(View.GONE);
        site.setVisibility(View.VISIBLE);
        name.setText(defaultSite.getUsername());
        userSite.setText(defaultSite.getAddress());
        phone.setText(defaultSite.getPhone());
    }



    @Override
    public void navMySite(){
        MySiteActivity.start(this);
    }

    @Override
    public void navPay(String orderStr){
        AliPayActivity.start(this, orderStr, 1);
    }

    @OnClick({R.id.add_site, R.id.site, R.id.choose, R.id.pay})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.add_site:
                navMySite();
                break;
            case R.id.site:
                navMySite();
                break;
            case R.id.pay:
                presenter.save();
                break;
        }
    }
}
